export type CloudFilterBarState = Record<string, any>

export default function usePopupPreGetData<
  T extends PromiseFn,
  U extends Record<string, any>,
>(fn?: T, field: string = 'cluster_id') {
  const visible = ref(false)
  const queryParams = ref<CloudFilterBarState>({})
  const record = ref<U | undefined>()
  const { data, loading, run, pagination } = useListRequest(fn, {
    params: queryParams,
    immediate: false,
  })
  watchEffect(() => {
    if (!visible.value) {
      record.value = undefined
    }
  })

  async function open(_record?: U) {
    record.value = _record
    queryParams.value[field] = _record?.id
    await run()
    visible.value = true
  }

  function close() {
    visible.value = false
  }

  return {
    loading,
    visible,
    record,
    pagination,
    data,
    run,
    open,
    close,
  }
}
